package uilayer;
import ctrlayer.*;
import modellayer.*;
import java.util.*;
import java.util.InputMismatchException;
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * Textbased UI which allows the user to input information about Vare.
 * 
 * @author Søren Kaae
 * @version 09.12.2014
 */
public class VareUI
{
    private VareCtr vCtr;
    private PersonCtr pCtr;
    private static VareUI instance;

    /**
     * Constructor for objects of class VareUI
     */
    private VareUI()
    {
        vCtr = new VareCtr();
        pCtr = new PersonCtr();
    }

    /**
     * Singleton Method getInstance
     *
     * Creates an instance of VareUI if one does not exist
     * 
     * @return instance of VareUI
     */
    public static VareUI getInstance()
    {
        if(instance == null)
        {
            instance = new VareUI();
        }
        return instance;
    }

    /**
     * Method loggetInd
     *
     * Calls the findLoggetInd method from the personcontroller. It takes a username as a parameter.
     * It returns the employeenumber of the person who is logged in at the moment.
     * 
     * @param brugernavn Username
     * @return Employeenumber
     */
    public String loggetInd(String brugernavn)
    {
        return pCtr.findLoggetInd(brugernavn);
    }    

    /**
     * Method vareMenu
     *
     * Checks for the userinput in the writeVareMenu method.
     * Each number results in different methods.
     */
    public void vareMenu(String loggetInd)
    {
        boolean exit = false;
        //trying to load wares
        while(!exit)//that is: while exit is false
        {
            int choice = writeVareMenu();
            switch(choice)
            {
                //create vare
                case 1: 
                System.out.println('\u000C');
                vCtr.createVare(inputNavn(), inputBeskrivelse(), inputPris(), inputStregkode(), inputBeholdning(), inputMinBeholdning(), inputMaxBeholdning());
                break;
                // print list over varer
                case 2:
                System.out.println('\u000C');
                vCtr.print();
                break;
                //find vare
                case 3:  
                System.out.println('\u000C');
                int v = inputVareID();
                if(vCtr.findVare(v) == null)
                {
                    System.out.println("Varen kunne desværre ikke findes. Prøv igen.");
                }
                else
                {
                    System.out.println(vCtr.findVare(v).toString());
                }
                break;
                // update vare
                case 4: 
                System.out.println('\u000C');
                System.out.println("\nInput vareID for den vare du vil redigere:");
                int vareID = inputVareID();
                if(vCtr.findVare(vareID) !=null)
                {
                    System.out.println("\nIndtast ny information");
                    String navn = inputNavn();
                    String beskrivelse = inputBeskrivelse();
                    int stregkode = inputStregkode();
                    int minBeholdning = inputMinBeholdning();
                    int maxBeholdning = inputMaxBeholdning();
                    vCtr.updateVare(vareID, navn, beskrivelse, stregkode, minBeholdning, maxBeholdning);
                    System.out.println("\nVare opdateret");
                }
                else
                {
                    System.out.println("\nVaren blev ikke fundet.");
                }
                break;
                // slet vare
                case 5:
                System.out.println('\u000C');
                vCtr.deleteVare(inputVareID());
                break;
                // ret pris (kun leder)
                case 6:
                vCtr.retPris(loggetInd, inputVareID(), inputPris());
                break;
                //load
                case 7:
                try
                {
                    vCtr.load();
                }
                catch(ClassNotFoundException e)
                {
                    System.out.println("file not found");
                }
                catch(IOException e)
                {
                    System.out.println("file not found");
                }
                System.out.println("file loaded");
                break;
                // save
                case 8:
                try
                {
                    vCtr.save();
                }
                catch(IOException e)
                {
                    System.out.println("file not found");
                }
                System.out.println("file saved"); 
                break;
                //exit
                default: exit = true;
            }
        }
    }

    public void save()
    {
        try
        {
            vCtr.save();
        }
        catch(IOException e)
        {
            System.out.println("Filen kunne ikke gemmes");
        }
        System.out.println("Filen er gemt");
    }

    public void loadVare()
    {
        try
        {
            vCtr.load();
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("Der skete en fejl under indlæsning af vare");
        }
        catch(IOException e)
        {
            System.out.println("Der skete en fejl under indlæsning af vare");
        }
        vCtr.vareIDCounterCheck();
    }

    /**
     * Method writeVareMenu
     *
     * Allows the user to create, find, update and delete wares.
     * 
     * @return An int based on one of the different choices.
     */
    private int writeVareMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\n*** VARE MENU ****");
        System.out.println(" (1) Tilføj vare");
        System.out.println(" (2) Print list over varer");
        System.out.println(" (3) Find vare");
        System.out.println(" (4) Rediger vare");
        System.out.println(" (5) Slet vare");
        System.out.println(" (6) Ret pris(kun leder)");
        System.out.println(" (7) Load vare");
        System.out.println(" (8) Save vare");
        System.out.println(" (9) Back");
        System.out.print("\n Choose: ");
        int choise;
        while(!keyboard.hasNextInt())
        {
            System.out.println("Indtast venligst et tal!");
            keyboard.next(); 
        }
        choise = keyboard.nextInt();
        System.out.print("\n");
        return choise;
    }

    /**
     * Method inputNavn
     *
     * @return name
     */
    public String inputNavn()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Varens navn:  ");
        String name = keyboard.nextLine();
        return name;
    }

    /**
     * Method inputBeskrivelse
     *
     * @return beskrivelse
     */
    public String inputBeskrivelse()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Varebeskrivelse:  ");
        String beskrivelse = keyboard.nextLine();
        return beskrivelse;   
    }

    /**
     * Method inputPris
     *
     * @return pris
     */
    public double inputPris()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Varens pris:  ");
        double pris = keyboard.nextDouble();
        return pris;
    }

    /**
     * Method inputStregkode
     *
     * @return stregkode
     */
    public int inputStregkode() 

    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Varens stregkode:  ");
        int stregkode = keyboard.nextInt();
        return stregkode;
    }

    /**
     * Method inputBeholdning
     *
     * @return beholdning
     */
    public int inputBeholdning()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Varens beholdning:  ");
        int beholdning = keyboard.nextInt();
        return beholdning;
    }

    /**
     * Method inputMinBeholdning
     *
     * @return beholdning
     */
    public int inputMinBeholdning()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Varens minimum beholdning:  ");
        int beholdning = keyboard.nextInt();
        return beholdning;
    }

    /**
     * Method inputMaxBeholdning
     *
     * @return beholdning
     */
    public int inputMaxBeholdning()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Varens maximum beholdning:  ");
        int beholdning = keyboard.nextInt();
        return beholdning;
    }

    /**
     * Method inputVareID
     *
     * @return id
     */
    public int inputVareID()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Varens ID:  ");
        int id = keyboard.nextInt();
        return id;
    }

}
